from iClouds.apiSerializers import *


class PayJingdianListSerializer(serializers.ModelSerializer):
    id = IntegerField(label="ID", read_only=True)
    basename = CharField(
        allow_blank=True, allow_null=True, max_length=255, help_text="全称"
    )
    nicename = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="简称",
    )
    aprice = IntegerField(allow_null=True, min_value=0, default=0, help_text="单价(分)")
    status = IntegerField(
        allow_null=True, max_value=1, min_value=0, default=1, help_text="状态"
    )
    parent = IntegerField(allow_null=True, help_text="父级")

    site = SerializerMethodField()
    info = SerializerMethodField()

    class Meta:
        model = PayJingdianList
        fields = "__all__"

    def get_site(self, attr):
        try:
            r = PaySpotList.objects.get(id=attr.parent)
        except BaseException as err:
            print(err)
            return None
        else:
            return ModelPaySpotListSerializer(r).data

    def get_info(self, attr):
        try:
            r = PayJingdianinfoList.objects.get(id=attr.parent)
        except BaseException as err:
            return None
        else:
            return ModelPayJingdianinfoListSerializer(r).data


# print(repr(PayJingdianListSerializer()))
